package com.fs.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.video.common.sysconst.SystemConst;
import com.fs.video.common.utils.FileUploadUtil;
import com.fs.video.common.utils.IdWorkerUtil;
import com.fs.video.common.utils.PasswordUtils;
import com.fs.video.mapper.UsersMapper;
import com.fs.video.pojo.Users;
import com.fs.video.service.UsersService;
import com.fs.video.vo.UsersVO;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    @Transactional
    public boolean register(String username, String password) {
        //1.判断用户名是否存在
        //条件类
        Users dbUsers = queryByName(username);

        if(dbUsers != null){ //说明数据库存在
            //2.如果存在, 无法注册
            return false;
        }
        //3.如果用户名不存在, 进行注册, 往数据库users 添加记录
        Users users = new Users();
        users.setId(String.valueOf(IdWorkerUtil.getNextId()));
        users.setUsername(username);
        users.setPassword(PasswordUtils.generate(password));
        users.setNickname(username);
        users.setFansCounts(0);
        users.setFollowCounts(0);
        users.setReceiveLikeCounts(0);
        usersMapper.insert(users);
        return true;
    }



    @Override
    @Transactional(propagation= Propagation.SUPPORTS)
    public UsersVO login(String username, String password) {
        //1.根据用户名查询用户信息
        Users dbUsers = queryByName(username);
        //2. 如果用户不存在, 返回null
        if(dbUsers == null){
            return null;
        }
        //3. 如果用户存在, 判断密码是否一样(密文比较)
        if(PasswordUtils.verify(password,dbUsers.getPassword())){
            //5.如果密码一样, 返回用户对象
            //把Users对象转换为UsersVO对象
            UsersVO  usersVO = new UsersVO();
            BeanUtils.copyProperties(dbUsers,usersVO);
            return usersVO;
        }else{
            //4.如果,密码不一样: 返回null
            return null;
        }
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS)
    public Users queryByName(String username) {
        QueryWrapper<Users>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Users dbUsers = usersMapper.selectOne(queryWrapper);
        return dbUsers;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public UsersVO queryById(String userId) {
        Users users = usersMapper.selectById(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users,usersVO);
        return usersVO;
    }

    @Override
    public String uploadFace(String userId, MultipartFile photo) {
        String savePath = SystemConst.SAVE_ROOT_PATH +userId+"/face";
        String savePathDb = "/"+userId+"/face";
        //1.先判断目录是否存在, 如果不存在, 创建目录
        FileUploadUtil.createPath(savePath);
        //2.保存文件到指定目录
        //获取上传的文件名
        String fileName = photo.getOriginalFilename();
        File svaeFile = new File(savePath+"/"+fileName);
        try {
            photo.transferTo(svaeFile);
            savePathDb = savePathDb +"/" +fileName;
            //3.修改数据库
            Users users = new Users();
            users.setId(userId);
            users.setFaceImage(savePathDb);
            usersMapper.updateById(users);
            //返回文件的路径
            return savePathDb;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
